package com.axsoft.jobimp.types;



/**
 * 
 * @author RankoR
 * 
 * short Type Length Data
 * 
 * Type: Word
 * Length: Word
 * Data: data with specified length
 */
public class sTLD {
	
	public static final int TYPE_OFFSET = 0x00;
	public static final int LENGTH_OFFSET = 0x02;
	public static final int DATA_OFFSET = 0x04;

	public Word mType;
	public Word mLength;
	public BLK mData;
	
	public final int getLength() {
		return mType.getLength() + mLength.getLength() + mLength.getValue();
	}
	
	public sTLD() {
		mType = new Word();
		mLength = new Word();
		mData = new BLK();
	}
	
	public sTLD(final BLK pBLK) {
		mType = new Word(pBLK.getWord(TYPE_OFFSET).getData());
		mLength = new Word(pBLK.getWord(TYPE_OFFSET).getData());
		mData = new BLK(pBLK.copyBytes(DATA_OFFSET, mLength.getValue()).getData());
	}
	
	public sTLD(final short pType, final byte[] pData) {
		mType = new Word(pType);
		
		short length;
		if (pData == null ) {
			length = 0;
		} else {
			length = (short) pData.length;
		}
		
		mLength = new Word(length);
		mData = new BLK(pData);
	}
	
	public sTLD(final Word pType, final BLK pData) {
		mType = pType;
		mLength = new Word((short) pData.getLength());
		mData = pData;
	}
	
	public final BLK toBLK() {
		final BLK blk = new BLK();
		
		blk.addBytes(mType.getData());
		blk.addBytes(mLength.getData());
		blk.addBytes(mData.getData());
		
		return blk;
	}
	
	
}
